import AppConfig from "../config/AppConfig";
import { Logger } from "../log/logger";

export default class HttpRequest {

    private http: Laya.HttpRequest;
    private url: string;

    create() {
        let http = this.http = new Laya.HttpRequest();
        this.url = AppConfig.url;
        http.on(Laya.Event.COMPLETE, this, this.onComplete);
        http.on(Laya.Event.ERROR, this, this.onError);
        http.on(Laya.Event.PROGRESS, this, this.onProgress);
    }

    /**
     * 
     * @param parme 
     */
    send(param: any, method?: string) {
        let params = this.getParams(param);
        Logger.logNet(params, "Request");

        this.http.send(this.url, param, method, "text",)
    }


    private getParams(param: any) {
        param = param ? param : Object.create(null);
        return JSON.stringify(param);
    }

    private onComplete(res: any) {
        Logger.logNet(res, "Response");
    }

    private onError(err: any) {
        Logger.logNet(err, "Error");

    }

    private onProgress(prs: any) {

    }
}